﻿package blob.levelstates
{
    import blob.*;
    import flash.net.*;
    import org.flixel.*;

    public class FinishedLevelState extends FlxState
    {
        private var thisLevel:FlxState;
        private var nextLevel:FlxState;
        private var thisLevelClass:Class;
        private var nextLevelClass:Class;
        private var results:FlxText;
        private var cont:FlxText;
        private var SO:SharedObject;
        private var QID:int;
        private var Bg:Class;
        private var Sr:Class;
        private var WinMusic:Class;
        private var Applause:Class;

        public function FinishedLevelState(param1:Class, param2:Class, param3:Number, param4:int, param5:int, param6:int)
        {
            var _loc_8:String = null;
            this.Bg = FinishedLevelState_Bg;
            this.Sr = FinishedLevelState_Sr;
            this.WinMusic = FinishedLevelState_WinMusic;
            this.Applause = FinishedLevelState_Applause;
            var _loc_7:* = new FlxSprite(0, 0, this.Bg);
            add(_loc_7);
            this.SO = SharedObject.getLocal("MyGame");
            if (param6 != 20)
            {
                this.SO.data.nextLevel = param6 + 1;
            }
            else
            {
                this.SO.data.nextLevel = 0;
            }
            this.QID = param6;
            if (param3 <= param5)
            {
                this.drawStars(3);
                if (FlxG.levels[this.QID] == null || FlxG.levels[this.QID] < 3)
                {
                    FlxG.levels[this.QID] = 3;
                }
                _loc_8 = "All stars obtained!";
            }
            else if (param3 <= param4)
            {
                this.drawStars(2);
                if (FlxG.levels[this.QID] == null || FlxG.levels[this.QID] < 2)
                {
                    FlxG.levels[this.QID] = 2;
                }
                _loc_8 = "Next Star: " + FlxU.formatTime(param5, true);
            }
            else
            {
                this.drawStars(1);
                if (FlxG.levels[this.QID] == null || FlxG.levels[this.QID] < 1)
                {
                    FlxG.levels[this.QID] = 1;
                }
                _loc_8 = "Next Star: " + FlxU.formatTime(param4, true);
            }
            this.SO.data.levels = FlxG.levels;
            this.SO.flush();
            this.thisLevelClass = param1;
            this.nextLevelClass = param2;
            var _loc_9:* = "Time: " + FlxU.formatTime(param3, true) + "\n" + _loc_8;
            var _loc_10:* = "Press SPACE to continue." + "\nPress \"R\" to restart level." + "\nPress \"M\" to select level.";
            this.cont = new FlxText(565, 500, 235, _loc_10);
            this.cont.setFormat(null, 14);
            add(this.cont);
            this.results = new FlxText(200, 150, 800, _loc_9);
            this.results.setFormat(null, 24);
            add(this.results);
            return;
        }// end function

        override public function create() : void
        {
            super.create();
            var _loc_1:* = FlxG.play(this.WinMusic, 0.5, true, true);
            _loc_1.fadeIn(0.5);
            return;
        }// end function

        public function drawStars(param1:int) : void
        {
            var _loc_2:FlxSprite = null;
            var _loc_3:FlxSprite = null;
            var _loc_4:FlxSprite = null;
            if (param1 > 0)
            {
                _loc_2 = new FlxSprite(200, 215, this.Sr);
                add(_loc_2);
            }
            if (param1 > 1)
            {
                _loc_3 = new FlxSprite(320, 215, this.Sr);
                add(_loc_3);
            }
            if (param1 > 2)
            {
                _loc_4 = new FlxSprite(440, 215, this.Sr);
                add(_loc_4);
            }
            return;
        }// end function

        override public function update() : void
        {
            if (FlxG.keys.SPACE)
            {
                this.nextLevel = new this.nextLevelClass();
                FlxG.switchState(this.nextLevel);
                this.kill();
            }
            else if (FlxG.keys.justPressed("R"))
            {
                this.thisLevel = new this.thisLevelClass();
                FlxG.switchState(this.thisLevel);
                this.kill();
            }
            else if (FlxG.keys.justPressed("M"))
            {
                FlxG.switchState(new GameMenu());
                this.kill();
            }
            return;
        }// end function

    }
}
